package com.aplink.generic.xmlparser.ctrlformat;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.text.TextUtils;

import com.aplink.generic.constant.MenuConstant;
import com.aplink.generic.pojo.controlspec.gridview.GridViewSpec;
import com.aplink.generic.pojo.controlspec.menu.MenuItemSpec;
import com.aplink.generic.xmlparser.MenuItemParser;
import com.aplink.generic.xmlparser.XmlPaserUtil;

public class GridViewFormat {
	private static final String AUTO_REFRESH = "autoRefresh";
	private static final String CONTENTDATA = "contentdata";
	private static final String EMPTY_ICON = "emptyIcon";
	private static final String EMPTY_MESSAGE = "emptyMessage";
	private static final String ROWITEM = "rowitem";
	private static final String RPTRDESIGN = "rptrdesign";
	private static final String SWIPE_TITLE = "swipeTitle";
	private static final String SWIPE_TO_REFRESH = "swipeRefreshData";
	private static final String SWIPE_TO_REMOVE = "swipeRemoveData";

	public static GridViewSpec parse(final Node node) {
		final GridViewSpec gridViewSpec = new GridViewSpec();
		if (node.hasAttributes()) {
			String value = XmlPaserUtil.getAttrValue(node, ROWITEM);
			if (!TextUtils.isEmpty(value)) {
				gridViewSpec.setRowItem(value);
			}

			value = XmlPaserUtil.getAttrValue(node, RPTRDESIGN);
			if (!TextUtils.isEmpty(value)) {
				gridViewSpec.setRptrdesign(value);
			}

			value = XmlPaserUtil.getAttrValue(node, CONTENTDATA);
			if (!TextUtils.isEmpty(value)) {
				gridViewSpec.setContentData(value);
			}
			value = XmlPaserUtil.getAttrValue(node, SWIPE_TO_REMOVE);
			if (!TextUtils.isEmpty(value)) {
				gridViewSpec.setSwipeToRemove(value.equalsIgnoreCase("Y"));
			}
			value = XmlPaserUtil.getAttrValue(node, AUTO_REFRESH);
			if (!TextUtils.isEmpty(value)) {
				gridViewSpec.setAutoRefresh(value.equalsIgnoreCase("Y"));
			}

			value = XmlPaserUtil.getAttrValue(node, EMPTY_MESSAGE);
			if (!TextUtils.isEmpty(value)) {
				gridViewSpec.setEmptyMessage(value);
			}
			value = XmlPaserUtil.getAttrValue(node, EMPTY_ICON);
			if (!TextUtils.isEmpty(value)) {
				gridViewSpec.setEmptyIcon(value);
			}
			value = XmlPaserUtil.getAttrValue(node, SWIPE_TO_REFRESH);
			if (!TextUtils.isEmpty(value)) {
				gridViewSpec.setSwipeToRefresh(value.equalsIgnoreCase("Y"));
				gridViewSpec.setAutoRefresh(value.equalsIgnoreCase("Y"));
			}

			value = XmlPaserUtil.getAttrValue(node, SWIPE_TITLE);
			if (!TextUtils.isEmpty(value)) {
				gridViewSpec.setSwipeToRefreshTitle(value);
			}
		}
		Element elementFormat = (Element) node;
		NodeList nodeListFormat = elementFormat
		        .getElementsByTagName(MenuConstant.SUB_MENU);
		for (int i = 0; i < nodeListFormat.getLength(); i++) {
			final Node nodeItem = nodeListFormat.item(i);
			final MenuItemSpec popupMenuItem = MenuItemParser
			        .parseMenuItemSpec(nodeItem);
			gridViewSpec.getContextMenus().add(popupMenuItem);
		}
		return gridViewSpec;
	}
}
